<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Category extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('Category_model');
        $this->load->model('Model_model');
    }

    public function main()
    {
        $categoryList = $this->Category_model->lists(); // 获取数组

        $category_tree_to_view = category_tree_to_view($categoryList); // 转换格式
                                                                       // 赋值
        $data = array(
            'category_tree_to_view' => $category_tree_to_view
        );
        $this->load->view('bm/category/main', $data);
    }

    /**
     * 新增分类
     */
    public function create()
    {
        if ($_POST) {
            $data = array(
                'title' => $this->input->post('title'),
                'name' => $this->input->post('name'),
                'image' => $this->input->post('image'),
                'model' => $this->input->post('model'),
                'upid' => $this->input->post('upid'),
                'status' => $this->input->post('status'),
                'keyword' => $this->input->post('keyword'),
                'description' => $this->input->post('description'),
                'lists_tpl' => $this->input->post('lists_tpl'),
                'detail_tpl' => $this->input->post('detail_tpl')
            );
            $res = $this->Category_model->insert($data);
            if ($res) {
                $msg = array(
                    'message' => '添加成功',
                    'waitSecond' => 1,
                    'jumpUrl' => '/bm/category/main'
                );
                $this->load->view('bm/common/success', $msg);
            }
        } else {
            // 获取数组
            $categoryList = $this->Category_model->lists(); 
            // 转换格式
            $tree_to_select = tree_to_select($categoryList); 
            // 获取模型
            $model_list = $this->Model_model->get_document_model();
            // 赋值
            $data = array(
                'tree_to_select' => $tree_to_select,
                'model_list' => $model_list
            );
            $this->load->view('bm/category/create', $data);
        }
    }

    /**
     * 修改分类
     */
    public function update()
    {
        if ($_POST) {
            $id = $this->input->post('id');
            $data = array(
                'title' => $this->input->post('title'),
                'name' => $this->input->post('name'),
                'image' => $this->input->post('image'),
                'is_check' => $this->input->post('is_check'),
                'upid' => $this->input->post('upid'),
                'status' => $this->input->post('status'),
                'keyword' => $this->input->post('keyword'),
                'description' => $this->input->post('description'),
                'lists_tpl' => $this->input->post('lists_tpl'),
                'detail_tpl' => $this->input->post('detail_tpl')
            );
            $res = $this->Category_model->update($id, $data);
            if ($res) {
                $msg = array(
                    'message' => '修改成功',
                    'waitSecond' => 1,
                    'jumpUrl' => '/bm/category/main'
                );
                $this->load->view('bm/common/success', $msg);
            }
        } else {

            // 获取详情
            $id = $this->input->get('id') ?? $this->uri->segment(5);
            $info = $this->Category_model->info($id);
            $categoryList = $this->Category_model->lists(); // 获取数组
            $tree_to_select = tree_to_select($categoryList); // 转换格式
            // 获取模型
            $model_list = $this->Model_model->get_document_model();
            // 赋值
            $data = array(
                'info' => $info,
                'tree_to_select' => $tree_to_select,
                'model_list' => $model_list
            );

            $this->load->view('bm/category/update', $data);
        }
    }
}
